//
//  HYNotifactionViewController.m
//  HYChatProject
//
//  Created by erpapa on 16/5/15.
//  Copyright © 2016年 erpapa. All rights reserved.
//

#import "HYMineSetViewController.h"
#import "HYUtils.h"
#import "HYXMPPManager.h"
#import "HYChangePasswordViewController.h"

@interface HYMineSetViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *logoutBtn;

@end

@implementation HYMineSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    self.logoutBtn.frame = CGRectMake(17, 175, VERTICAL_SCREEN_WIDTH-34, 45);
    [self.view addSubview:self.logoutBtn];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellIdentifier=@"kCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"清除缓存";
        
    } else if (indexPath.section == 1) {
        cell.textLabel.text = @"修改密码";
        
    } else if (indexPath.section == 2) {
        cell.textLabel.text = @"";
        cell.backgroundColor = [UIColor clearColor];
    }
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        //清除缓存
        [HYUtils showWaitingMsg:@"请稍候..."];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            BOOL delChat = [[HYDatabaseHandler sharedInstance] deleteAllChatMessage];
            BOOL delGroupChat = [[HYDatabaseHandler sharedInstance] deleteAllGroupChatMessage];
            BOOL delRecentChat = [[HYDatabaseHandler sharedInstance] deleteAllRecentChats];
            if (delChat && delGroupChat && delRecentChat) {
                [HYUtils alertWithNormalMsg:@"缓存已清空"];
            }else if (!delRecentChat){
                [HYUtils alertWithNormalMsg:@"清除聊天记录失败，请重试"];
            }else if (delChat || delGroupChat){
                [HYUtils alertWithNormalMsg:@"已清空部分聊天缓存"];
            }else{
                [HYUtils alertWithNormalMsg:@"已清空部分聊天缓存"];
            }
        });
        
    } else if (indexPath.section == 1) {
        
        HYChangePasswordViewController *vc = [[HYChangePasswordViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        
    } else if (indexPath.section == 2) {
        
        [HYUtils showWaitingMsg:@"请稍候..."];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [HYUtils clearWaitingMsg];
            [[HYXMPPManager sharedInstance] xmppUserlogout]; // 退出登录
        });
    }
}

- (void)switchChange:(UISwitch *)sender
{
    if (sender.tag == 0) {
        [[NSUserDefaults standardUserDefaults] setBool:sender.on forKey:HYChatShieldNotifaction];
    } else if (sender.tag == 1) {
        [[NSUserDefaults standardUserDefaults] setBool:!sender.on forKey:HYChatNotShowBody];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIButton *)logoutBtn
{
    if (!_logoutBtn) {
        _logoutBtn   = [UIButton buttonWithType:UIButtonTypeCustom];
        _logoutBtn.layer.cornerRadius = 8.0;
        _logoutBtn.backgroundColor = APP_MAIN_COLOR;
        [_logoutBtn setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
        [_logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [_logoutBtn addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logoutBtn;
}

-(void)logoutAction
{
    [HYUtils showWaitingMsg:@"请稍候..."];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [HYUtils clearWaitingMsg];
        [[HYXMPPManager sharedInstance] xmppUserlogout]; // 退出登录
    });
    
}

@end
